<?php
class Puzzle extends Model
{
	/**
	*
	* @var User
	*/
	private $user;

	/**
	*
	* @return User
	*/
	public function getUser()
	{
		if(!$this->user)
		{
			$this->user = new User($this->user_id);
		}	
		return $this->user;
	}
	
	/**
	 * @param int $userId
	 * @return Solution
	 * 
	 */
	public function getIsSolvedByUser($userId)
	{
		$oInstance = Solution::getSingle(array('puzzle_id' => $this->getId(), 'user_id' => $userId, 'confirmed' => 'CONFIRMED'), 'date', 'asc');
		if($oInstance)
		{
			return $oInstance;
		}
		return false;
	}
	/**
	 * @param int $userId
	 * @return Solution
	 * 
	 */
	public function userHasPendingSolution($userId)
	{
		$oInstance = Solution::getSingle(array('puzzle_id' => $this->getId(), 'user_id' => $userId, 'confirmed' => 'NOTCONFIRMED'), 'date', 'asc');
		if($oInstance)
		{
			return $oInstance;
		}
		return false;
	}
	
	public function getHints()
	{
		return Hint::getAll(array('puzzle_id' => $this->getId()), 'orderweigh', 'asc');
	}

	/**
	 * 
	 * @param array $where
	 * @param string $orderBy
	 * @param string $dir
	 * @param int $limit
	 * @return array
	 */
	public static function getAll($where = array(), $orderBy = null, $dir = 'ASC', $limit = 0)
	{
		return self::getAllModels(get_class(), $where, $orderBy, $dir, $limit);
	}
}